package com.preskool.controller;

import com.github.pagehelper.PageHelper;
import com.preskool.dto.ActionResult;
import com.preskool.po.Salary;
import com.preskool.service.SalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 *
 * @Author swj
 * @Date 2021/1/6
 * @version1.0
 */
@RestController
public class SalaryController {
    //注入SalaryService接口
    @Autowired(required = false)
    private SalaryService salaryService;


    /**
     * 查询全部数据,并进行分页展示
     * 薪水页面
     * @return
     */
    @RequestMapping(value = "/findAllSalary",method = RequestMethod.GET)
    //默认为第一页,每页显示十条数据
    public ActionResult findAll(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "10") Integer pageSize){

        PageHelper.startPage(currentPage,pageSize);
        List<Salary> all = salaryService.findAllPaging(currentPage,pageSize);

        /*PageInfo<Salary> pif = new PageInfo<Salary>(all);
        System.out.println("每页显示的条数:"+pif.getPageSize());
        System.out.println("当前页码:"+pif.getPageNum());
        System.out.println("总记录数:"+pif.getTotal());
        System.out.println("总页数:"+pif.getPages());*/

        if(all!=null){
            return new ActionResult(200,"查询成功",all);
        }
        return new ActionResult(204,"查询失败",null);
    }

    /**
     * 加薪页面:方案一:通过id查询数据,展示在前端页面,再通过提交进行加薪
     *        方案二:直接在前端页面输入数据,点击提交进行修改
     * 通过salid查询出对应的数据
     * 加薪页面方案一:
     * @param salid
     * @return
     */
    @RequestMapping(value = "/findOneSalary",method = RequestMethod.GET)
    public ActionResult findPart( Integer salid) {
        Salary part = salaryService.findPart(salid);
        if (part != null) {
            return  new ActionResult(200, "successful", part);
        }
        return new ActionResult(404,"fail",null);
    }

    /**
     * 加薪页面:单独修改amount数据
     * 通过id修改数据
     * @param salary
     * @return
     */
    @RequestMapping(value = "/updateSalary",method = RequestMethod.POST)
    public ActionResult add(Salary salary){
        int i = salaryService.updateSalary(salary);
        if(i>-1){
            return new ActionResult(200,"恭喜您,已为您加薪",null);
        }
        return new ActionResult(203,"加薪失败,请继续努力",null);
    }

    /**
     * 添加数据
     *  将前端页面输入数据,添加到数据表中
     * @param salary
     * @return
     */
    @RequestMapping(value = "/addSalary",method = RequestMethod.POST)
    public ActionResult addSalary(Salary salary){
        int i = salaryService.addSalary(salary);
        if(i>-1){
            return new ActionResult(200,"数据插入成功",null);
        }
        return  new ActionResult(203,"数据插入失败",null);
    }

    /**
     * 根据id删除单条数据
     * @param salid
     * @return
     */
    @RequestMapping(value = "/delSalary",method = RequestMethod.GET)
    public ActionResult delSalary(Integer salid){
        int del = salaryService.delSalary(salid);
        if(del>-1){
            return new ActionResult(200,"删除成功",null);
        }
        return new ActionResult(203,"删除失败",null);
    }


}
